package com.test.springCloud.controller;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.test.springCloud.service.DeptService;
import com.test.springcloud.pojo.DeptPojo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务者控制类
 */
@Controller
@CrossOrigin  //跨域
@RequestMapping("/lxf")
@Slf4j
public class DeptController {

    @Resource
    DeptService deptService;

    /**
     * 添加部门信息
     * @param req
     * @return
     * @throws JSONException
     */
    @ResponseBody
    @PostMapping("/InsertDept")
    public String InsertDept(@RequestBody JSONObject req) throws JSONException {

        JSONObject js = new JSONObject();

        DeptPojo deptPojo = new DeptPojo();
        String dept_name = req.getString("dept_name");
        //添加进实体类中
        deptPojo.setDept_name(dept_name);

        //添加部门信息
        Integer code = deptService.insertDept(deptPojo);
        if (code>0){
           js.put("code",200);
           js.put("msg","数据添加成功！");
        }else {
            js.put("code",200);
            js.put("msg","数据添加失败！");
        }

        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }

    /**
     * 获取部门信息
     * @return
     * @throws JSONException
     */
    @GetMapping("/GetAllDept")
    @ResponseBody
    public String getAllDept() throws JSONException {

        JSONObject js = new JSONObject();
        List<DeptPojo> list = deptService.getAllDept();

        js.put("code",200);
        js.put("msg","数据获取成功！");
        js.put("data",list);

        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }
}
